<template>
  <div>
    <div>
      <toption :crops="names" @scrop="scrop" @sma="sma" @spre="spre"/>
    </div>
    <div>
      <Row>
        <Col span="12">
          <dataline :datas="datas" title1="原始数据" title2="N-MA" />
        </Col>
        <Col span="12">
          <Table :columns="columns10" :data="data10" border></Table>
        </Col>
      </Row>
    </div>
  </div>
</template>

<script>
import dataline from "./charts/dataline.vue";
import toption from './charts/tprice.vue'
 export default {
  data() {
    return {
      datas: [[], [], [], []],
      datas1: [],
      datestart: "",
      dateend: "",
      cropname: "",
      cycle: "",
      names:[],
      columns10: [
        {
          title: "预测情况汇总",
          align: "center",
          children: [
            {
              title: "时间",
              align: "center",
              key: "dates",
            },
            {
              title: "最小值",
              align: "center",
              key: "min",
            },
            {
              title: "均值",
              align: "center",
              key: "avg",
            },
            {
              title: "最大值",
              align: "center",
              key: "max",
            },
          ],
        },
      ],
      data10: [],
      
    };
  },
  components: {
    dataline,
    toption,
  },
  mounted() {
    this.axios
      .get("/api/analysis")
      .then((r) => {
        this.datas = r.data.charts;
        this.data10=r.data.table;
        this.names=r.data.names
      })
      .catch((e) => {
        this.$Message.error("e" + e);
      });
  },
  methods: {
    scrop(crop) {
      var data1={crop:crop}
      this.axios
        .get("/api/analysis", { params: { param: data1 } })
        .then((r) => {
          this.datas = r.data.charts;
          this.data10=r.data.table;
        })
        .catch((e) => {
          this.$Message.error("e" + e);
        });
    },
    sma(num) {
      var res = [["MA_date"], ["MA"]];
      var i, it, k;
      
      if (num % 2 == 0) {
        k = num / 2;
        for (i = k; i < this.datas[0].length - k - 1; i++) {
          one = 0;
          for (it = -k; it <= k; it++) {
            if (it == -k || it == k) {
              one += this.datas[3][it + i] / num;
            } else {
              one += (this.datas[3][it + i] / num) * 2;
            }
          }
          res[1].push(one / 2);
          res[0].push(this.datas[0][i]);
        }
      } else {
        k = (num - 1) / 2;

        for (i = k; i < this.datas[1].length - k - 1; i++) {
          var one = 0;
          for (it = -k; it <= k; it++) {
            one += this.datas[3][it + i];
          }
          res[1].push(one / num);
          res[0].push(this.datas[0][i]);
        }
      }
      this.datas.splice(2, 1, res[0]);
      this.datas.splice(3, 1, res[1]);
      // this.datas[2]=res[0]
      // this.datas[3]=res[1]
    },
    spre(n){
      this.axios.post("/api/analysis",n).then((r)=>{
        this.data10=r.data
      }).catch((e)=>{
        this.$Message.error("e"+e)
      })
    }
  },
};
</script>

<style>
</style>